<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/component/pear/css/pear.css"/>
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <label class="layui-form-label">部门名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="deptName" placeholder="请输入部门名称" class="layui-input">
                </div>
                <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="dept-query">
                    <i class="layui-icon layui-icon-search"></i>
                    查询
                </button>
                <button type="reset" class="pear-btn pear-btn-md">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="dept-table" lay-filter="dept-table"></table>
    </div>
</div>

<script type="text/html" id="dept-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
        <i class="layui-icon layui-icon-delete"></i>
        删除
    </button>
</script>

<script type="text/html" id="dept-bar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i>
    </button>
</script>


<script type="text/html" id="dept-status">
    <input type="checkbox" name="status" value="{{d.dept_id}}" lay-skin="switch" lay-text="启用|禁用" mid="{{d.deptId}}"
           lay-filter="dept-status" {{ d.status== '1' ? 'checked' : '' }}>
</script>

<script type="text/html" id="icon">
    <i class="layui-icon {{d.icon}}"></i>
</script>
<script type="text/html" id="dept-time">
    {{layui.util.toDateString(d.createTime, 'yyyy-MM-dd HH:mm:ss')}}
</script>
<script type="text/html" id="user-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
        <i class="layui-icon layui-icon-delete"></i>
        删除
    </button>
</script>
<script src="/component/layui/layui.js"></script>
<script src="/component/pear/pear.js"></script>
<script>
    layui.use(['table', 'form', 'jquery', 'common'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;

        let deptCols = [
            [
                {type: 'checkbox'},
                {title: '部门名称', field: 'dept_name', align: 'center'},
                {title: '启用', field: 'status', align: 'center', templet: '#dept-status'},
                {title: '排序', field: 'sort', align: 'center'},
                {title: '创建时间', field: 'create_time', align: 'center', sort: true},
                {title: '操作', toolbar: '#dept-bar', align: 'center', width: 180}
            ]
        ];

        tableIns = table.render({
            elem: '#dept-table',
            url: '/dept/list',
            page: true,
            cols: deptCols,
            skin: 'line',
            toolbar: '#dept-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports'],
            // height: "full-225",
            id: 'dept-table',
            //解决页码的问题
            done: function (res) {
                // 获得当前页码
                var brforeCurr = tableIns.config.page.curr;
                // 获得当前页的记录数
                var dataLength = table.cache['dept-table'].length;
                var count = res.count; // 获得总记录数
                //如果当前页的记录数为0并且总记录数不为0
                if (dataLength == 0 && count != 0) {
                    // 刷新表格到上一页
                    table.reload("dept-table", {
                        page: {
                            curr: brforeCurr - 1
                        }
                    });
                }
            }
        });

        table.on('tool(dept-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            }
        })
        // 刷新按钮
        window.refresh = function (param) {
            table.reload('dept-table');
        }
        table.on('toolbar(dept-table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            }
        });
        form.on('switch(dept-status)', function (obj) {
            layer.confirm(obj.elem.checked ? '确定要启用该部门吗' : '确定要停用该部门吗', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                var status = obj.elem.checked ? 1 : 0;
                var data = {
                    deptId: obj.value,
                    status: status
                }
                let loading = layer.load();
                $.ajax({
                    url: '/dept/status',
                    data: JSON.stringify(data),
                    dataType: 'json',
                    contentType: 'application/json',
                    type: 'put',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 200) {
                            layer.msg(result.message, {icon: 1, time: 3000}, function () {
                                layui.table.reload("#dept-table");
                            });
                        } else {
                            layer.msg(result.message, {icon: 2, time: 3000});
                            window.refresh();
                        }
                    }
                })
            });
        });
        form.on('submit(dept-query)', function (data) {
            table.reload('dept-table', {
                where: data.field
            })
            return false;
        });
        window.add = function () {
            layer.open({
                type: 2,
                title: '新增部门',
                shade: 0.1,
                area: ['450px', '500px'],
                content: '/dept/add'
            });
        }

        window.edit = function (obj) {
            var data = obj.data;
            layer.open({
                type: 2,
                title: '修改',
                shade: 0.1,
                area: ['450px', '500px'],
                content: '/dept/edit?deptId=' + data.dept_id
            });
        }
        window.remove = function (obj) {
            var data = obj.data;
            layer.confirm('确定删除吗,此操作不能撤销！', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/dept/delete/" + data.dept_id,
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code === 200) {
                            layer.msg(result.message, {icon: 1, time: 3000}, function () {
                                obj.del();
                                table.reload('dept-table');
                            });
                        } else {
                            layer.msg(result.message, {icon: 2, time: 3000});
                        }
                    }
                })
            });
        }

        // 批量删除
        window.batchRemove = function (obj) {
            var checkIds = common.checkField(obj, 'dept_id');
            if (checkIds === "") {
                layer.msg("未选中数据", {
                    icon: 3,
                    time: 1000
                });
                return false;
            }
            layer.confirm('确定要删除这些部门?', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/dept/delete/" + checkIds,
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code === 200) {
                            layer.msg(result.message, {icon: 1, time: 2000}, function () {
                                table.reload("dept-table");
                            });
                        } else {
                            layer.msg(result.message, {icon: 2, time: 2000});
                        }
                    }
                })
            });
        }
    })
</script>
</body>
</html>